package com.linhui.project.datasource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.linhui.project.common.ErrorCode;
import com.linhui.project.exception.BusinessException;
import com.linhui.project.exception.ThrowUtils;
import com.linhui.project.mapper.PostMapper;
import com.linhui.project.model.dto.post.PostQueryRequest;
import com.linhui.project.model.entity.Post;
import com.linhui.project.model.entity.User;
import com.linhui.project.model.vo.PostVO;
import com.linhui.project.service.PostService;
import com.linhui.project.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 帖子服务实现
 *
 * @author: 临晖
 * @since: 2024-05-18
 */
@Service
@Slf4j
public class PostDataSource  implements DataSource<PostVO> {

    private final static Gson GSON = new Gson();

    @Resource
    private PostService postService;



    @Override
    public Page<PostVO> doSearch(String searchText, long pageNum, long pageSize) {
        PostQueryRequest postQueryRequest = new PostQueryRequest();
        postQueryRequest.setSearchText(searchText);
        postQueryRequest.setCurrent(pageNum);
        postQueryRequest.setPageSize(pageSize);


        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        return postService.listPostVOByPage(postQueryRequest, request);

    }
}




